package com.syedu.day12;

import com.google.common.collect.Lists;
import com.syedu.day13.Car;

import java.util.Collections;
import java.util.List;

/**
 * description：匿名内部类
 * @author Administrator
 * @date 2022/3/31 9:55
 */
public class InnerClass {
    public static void main(String[] args) {
        Car car1 = new Car("Benz", "E300L", 400000.0);
        Car car2 = new Car("Bmw", "320Li", 250000.0);
        Car car3 = new Car("Audi", "Q7", 700000.0);
        Car car4 = new Car("Byd", "汉EV", 250000.0);
        Car car5 = new Car("Benz", "C200L", 250000.0);
        Car car6 = new Car("Audi", "A4L", 300000.0);
        Car car7 = new Car("Bmw", "M3", 800000.0);
        Car car8 = new Car("Benz", "EQS", 1500000.0);
        Car car9 = new Car("Audi", "e-tron", 700000.0);
        Car car10 = new Car("Audi", "R8", 2300000.0);
        List<Car> carList = Lists.newArrayList(car1, car2, car3, car4, car5, car6, car7, car8, car9, car10);

        /**
         *  匿名内部类的使用
         *  例如:Collections.sort(),Arrays.sort(),list.removeIf()...
         *
         *  注意: JDK8.0开始,可以把匿名内部类的语法改写成lambda表达式
         */
        /*carList.removeIf(new Predicate<Car>() {
            @Override
            public boolean test(Car car) {
                return car.getPrice() < 300000;
            }
        });*/

        //lambda表达式
        carList.removeIf(car -> car.getPrice() < 300000);

        /*Collections.sort(carList, new Comparator<Car>() {
            @Override
            public int compare(Car o1, Car o2) {
                return o2.getPrice().compareTo(o1.getPrice());
            }
        });*/

        //lambda表达式
        Collections.sort(carList, (o1, o2) -> o2.getPrice().compareTo(o1.getPrice()));

        carList.forEach(System.out::println);
    }
}

